<template>
  <div class="tb-login">
    <img
      v-show="isQrCode"
      @click="tabQrCode"
      src="../../assets/images/qr-code.png"
      alt=""
      class="qrcode"
    />
    <img
      v-show="!isQrCode"
      @click="tabQrCode"
      src="../../assets/images/password.png"
      alt=""
      class="qrcode"
    />
    <div v-show="isQrCode" class="tb-form-login">
      <div class="login-type">
        <span
          class="type-item"
          @click="tabLoginType"
          :class="[activeVal ? 'active-type' : '']"
        >
          密码登录
        </span>
        <span
          class="type-item"
          @click="activeVal = !activeVal"
          :class="[!activeVal ? 'active-type' : '']"
        >
          短信登录
        </span>
      </div>
      <div class="empty" v-show="!showError" style="height:20px;"></div>
      <p class="error-tip" v-show="showError">
        <i class="el-icon-warning"></i>
        <span>{{ errorTip }}</span>
      </p>
      <div class="pwd-login login-plain" v-show="activeVal">
        <div class="input-plain">
          <img
            src="../../assets/images/tb-user.png"
            alt=""
            class="input-icon"
          />
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="会员名/邮箱/手机号"
            name="username"
            type="text"
            tabindex="1"
          />
        </div>
        <div class="input-plain">
          <img src="../../assets/images/tb-pwd.png" alt="" class="input-icon" />
          <el-input
            ref="username"
            v-model="loginForm.password"
            placeholder="请输入登录密码"
            name="password"
            type="password"
            tabindex="1"
          />
        </div>
      </div>
      <div class="sms-login login-plain" v-show="!activeVal">
        <div class="input-plain">
          <img src="../../assets/images/phone.png" alt="" class="input-icon" />
          <span class="phone-type">+86</span>
          <i class="el-icon-d-caret caret"></i>
          <el-input
            ref="username"
            v-model="phoneForm.phone"
            placeholder="请输入手机号"
            name="username"
            type="text"
            tabindex="1"
          />
        </div>
        <div class="input-plain check-code">
          <img src="../../assets/images/sms.png" alt="" class="input-icon" />
          <el-input
            ref="username"
            v-model="phoneForm.checkCode"
            placeholder="请输入验证码"
            name="password"
            type="text"
            tabindex="1"
          />
          <span class="getCode">获取验证码</span>
        </div>
      </div>
      <el-button
        type="success"
        class="login-button"
        style="width:100%;"
        @click.native.prevent="handleLogin"
        >登录</el-button
      >
    </div>
    <div v-show="!isQrCode" class="tb-qrcode-login">
      <p class="master-login-title">
        手机扫码，安全登录
      </p>
      <div class="qrcode-wrap">
          <img src="../../assets/images/qrCode.png" alt="">
      </div>
      <div class="scan">
          <img src="../../assets/images/scan.png" alt=""></img>
      </div>
      <div class="bottom-links">
          <span @click="tabQrCode">密码登录</span>
      </div>
    </div>
    <div class="tip-wrap" v-show="isQrCode && activeVal && !showError">
      <span class="tips">忘记密码</span>
      <span class="tips">忘记用户名</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isQrCode: true,
      activeVal: true,
      showError: false,
      loginForm: {
        username: "",
        password: ""
      },
      phoneForm: {
        phone: "",
        checkCode: ""
      },
      errorTip: "请输入用户名"
    };
  },
  methods: {
    tabQrCode(){
        this.isQrCode = !this.isQrCode
        this.clear()
    },
    clear(){
        this.loginForm = {
        username: "",
        password: ""
      };
      this.phoneForm = {
        phone: "",
        checkCode: ""
      };
    },
    tabLoginType() {
      this.activeVal = !this.activeVal;
      this.showError = false;
      this.clear()
    },
    handleLogin() {
      if (this.activeVal) {
        if (!this.loginForm.username) {
          this.showError = true;
          this.errorTip = "请输入账户名";
        } else if (!this.loginForm.password) {
          this.showError = true;
          this.errorTip = "请输入密码";
        } else {
          this.showError = false;
          // login
        }
      }
      if (!this.activeVal) {
        if (!this.phoneForm.phone) {
          this.showError = true;
          this.errorTip = "请输入手机号码";
        } else if (!this.phoneForm.checkCode) {
          this.showError = true;
          this.errorTip = "请先发送短信验证码";
        } else {
          this.showError = false;
          // login
        }
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.tb-login {
  color: #6c6c6c;
  background: #fff;
  position: relative;
  margin: 0 5px;
  .qrcode {
    position: absolute;
    top: 2px;
    right: 0;
    width: 52px;
    cursor: pointer;
  }
  .tip-wrap {
    position: absolute;
    bottom: 0;
    right: 30px;
    .tips {
      font-size: 12px;
      margin-left: 10px;
      color: #30b30e;
      cursor: pointer;
    }
  }
  .tb-form-login {
      padding: 45px 25px 23px;
    .login-type {
      margin-top: 13px;
      color: #30b30e;
      .type-item {
        font-size: 16px;
        font-weight: 700;
        border-bottom: 2px solid transparent;
        margin-right: 10px;
        padding-bottom: 4px;
        text-align: center;
        cursor: pointer;
        display: inline-block;
      }
      .active-type {
        border-color: #000;
      }
    }
    .login-plain {
      .input-plain {
        width: 100%;
        height: 42px;
        border: 1px solid #f1eeee;
        font-size: 14px;
        box-sizing: border-box;
        padding-left: 10px;
        margin-bottom: 20px;
        .input-icon {
          width: 20px;
        }
        .phone-type {
          display: inline-block;
          width: 70px;
          text-align: center;
          font-size: 15px;
        }
        .getCode {
          color: #30b30e;
          font-size: 12px;
          cursor: pointer;
        }
      }
    }
    .sms-login {
      .input-plain {
        .input-icon {
          width: 22px;
        }
      }
    }
    .error-tip {
      margin-bottom: 10px;
      margin-top: 10px;
      padding: 5px 8px;
      border: 1px solid transparent;
      overflow: hidden;
      background-color: #fef2f2;
      font-size: 12px;
      color: #666;
      display: flex;
      align-items: center;
      border: 1px solid #ffb4a8;
      .el-icon-warning {
        color: #f50;
        font-size: 18px;
        margin-right: 4px;
      }
    }
  }
  .tb-qrcode-login {
      padding: 25px 25px 0;
    .master-login-title {
      height: 18px;
      line-height: 18px;
      font-size: 16px;
      color: #3c3c3c;
      margin-top: 9px;
      padding-bottom: 8px;
      font-weight: 700;
    }
    .qrcode-wrap {
        text-align: center;
        box-shadow: 0 0 8px #ddd;
        width:140px;
        height: 140px;
        margin: 20px auto;
        margin-top:30px;
        img {
            width:100%;
        }
    }
    .scan {
        width:188px;
        margin:0 auto;
        img {
            width:34px;
        }
    }
    .bottom-links {
        margin: 8px 0;
        text-align: right;
        zoom: 1;
        font-size: 12px;
      color: #30b30e;
      cursor: pointer;
    }
  }
}
</style>

<style lang="scss">
.tb-login {
  .el-input input {
    height: 42px;
    padding-top: 2px;
    &::-webkit-input-placeholder {
      color: #757575;
    }
  }
  .el-button--success {
    font-size: 16px;
    margin-bottom: 29px;
  }
  .sms-login {
    .el-input input {
      padding-left: 5px;
    }
    .check-code {
      .el-input {
        width: 66%;
      }
      .el-input input {
        padding-left: 15px;
      }
    }
    .el-input {
      width: 62%;
    }
  }
}
</style>
